package com.anosine.gpsl.android.util;

import android.app.Activity;

import java.lang.ref.WeakReference;
import java.util.LinkedList;
import java.util.List;

import com.anosine.gpsl.android.ui.activity.GPSLMainActivity;
/**
 * @Title: ActivityFrgManager
 * @Package com.anosine.gpsl.android.util
 * @Description: 
 * @author: sunny
 * @date: 2015-11-24
 * @version V1.0
 */
public class ActivityFrgManager {

    public static ActivityFrgManager sManager;

    public static ActivityFrgManager getInstance(){
        if (sManager==null){
            sManager=new ActivityFrgManager();
        }
        return sManager;
    }

    private final List<WeakReference<Activity>> mActivities=new LinkedList<WeakReference<Activity>>();

    public void registerActivity(Activity activity) {
        mActivities.add(new WeakReference<Activity>(activity));
    }

    public void clearActivityAdd() {
        mActivities.clear();
    }

    public void finishActivities() {
        for (WeakReference<Activity> activity : mActivities) {
            Activity a=activity.get();
            if (null!=a && !a.isFinishing()) {
                a.finish();
            }
        }
        clearActivityAdd();
    }

    public List<WeakReference<Activity>> getActivities() {
        return mActivities;
    }

    public GPSLMainActivity getLastActivityIsMain(){
        int number=mActivities!=null? mActivities.size(): 0;
        if (number>1){
            Activity activity=mActivities.get(number-1).get();
            if (null!=activity && !activity.isFinishing() && activity instanceof GPSLMainActivity) {
                GPSLMainActivity m= (GPSLMainActivity) activity;
                return m;
            }
        }
        return null;
    }
}
